package com.liangsui.mss.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liangsui.mss.constants.Constants;
import com.liangsui.mss.domain.Role;
import com.liangsui.mss.dto.RoleDto;
import com.liangsui.mss.mapper.RoleMapper;
import com.liangsui.mss.service.RoleService;
import com.liangsui.mss.vo.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
* @author zhang
* @description 针对表【sys_role(角色信息表)】的数据库操作Service实现
* @createDate 2022-12-05 16:41:03
*/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService{
    @Autowired
    private RoleMapper roleMapper;

    /**
     * 分页查询角色
     *
     * @param roleDto
     * @return
     */
    @Override
    public DataGridView listRolePage(RoleDto roleDto) {
        Page<Role> page = new Page<>(roleDto.getPageNum(), roleDto.getPageSize());
        QueryWrapper<Role> qw = new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(roleDto.getRoleName()),
                Role.COL_ROLE_NAME, roleDto.getRoleName());
        qw.like(StringUtils.isNotBlank(roleDto.getRoleCode()),
                Role.COL_ROLE_CODE, roleDto.getRoleCode());
        qw.eq(StringUtils.isNotBlank(roleDto.getStatus()),
                Role.COL_STATUS, roleDto.getStatus());
        qw.ge(null != roleDto.getBeginTime(),
                Role.COL_CREATE_TIME, roleDto.getBeginTime());
        qw.le(null != roleDto.getEndTime(),
                Role.COL_CREATE_TIME, roleDto.getEndTime());
        qw.orderByAsc(Role.COL_ROLE_SORT);
        this.roleMapper.selectPage(page, qw);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    /**
     * 查询所有可用角色
     *
     * @return
     */
    @Override
    public List<Role> listAllRoles() {
        QueryWrapper<Role> qw = new QueryWrapper<>();
        qw.eq(Role.COL_STATUS, Constants.STATUS_TRUE);
        qw.orderByAsc(Role.COL_ROLE_SORT);
        return this.roleMapper.selectList(qw);
    }

    /**
     * 根据ID查询角色
     *
     * @param roleId
     * @return
     */
    @Override
    public Role getRoleById(Long roleId) {
        return this.roleMapper.selectById(roleId);
    }

    /**
     * 添加一个角色
     *
     * @param roleDto
     * @return
     */
    @Override
    public int addRole(RoleDto roleDto) {
        Role role = new Role();
        BeanUtil.copyProperties(roleDto, role);
        // 设置创建者
        role.setCreateBy(roleDto.getSimpleUser().getUserName());
        // 设置创建时间
        role.setCreateTime(DateUtil.date());
        return this.roleMapper.insert(role);
    }

    /**
     * 修改角色
     *
     * @param roleDto
     * @return
     */
    @Override
    public int updateRole(RoleDto roleDto) {
        Role role = new Role();
        BeanUtil.copyProperties(roleDto, role);
        // 设置修改者
        role.setCreateBy(roleDto.getSimpleUser().getUserName());
        // 设置修改时间
        role.setUpdateTime(DateUtil.date());
        return this.roleMapper.updateById(role);
    }

    /**
     * 根据角色ID删除角色
     *
     * @param roleIds
     * @return
     * @RBAC思路：删除菜单数据也要删除与之关联的中间表数据
     */
    @Override
    public int deleteRoleByIds(Long[] roleIds) {
        List<Long> idList = Arrays.asList(roleIds);
        if (idList != null && !idList.isEmpty()) {
            // 根据角色id删除sys_role_menu表数据
            this.roleMapper.deleteRoleIdByMenu(idList);
            // 根据角色id删除sys_role_user表数据
            this.roleMapper.deleteRoleIdByUser(idList);
            // 删除角色
            return this.roleMapper.deleteBatchIds(idList);
        }
        return 0;
    }

    /**
     * @param roleId
     * @param menuIds
     * @功能描述:保存角色和菜单关系
     */
    @Override
    public void saveRoleMenu(Long roleId, Long[] menuIds) {
        // 根据角色ID删除role_menu数据
        this.roleMapper.deleteRoleIdByMenu(Arrays.asList(roleId));
        for (Long menuId : menuIds) {
            this.roleMapper.saveRoleMenu(roleId,menuId);
        }
    }

    /**
     * 根据用户ID查询用户拥有的角色IDS
     * @param userId
     * @return
     */
    @Override
    public List<Long> getRoleIdsByUserId(Long userId) {
        if(null==userId){
            return Collections.EMPTY_LIST;
        }
        return this.roleMapper.selectRoleIdsByUserId(userId);
    }

    @Override
    public void saveRoleUser(Long userId, Long[] roleIds) {
        //根据用户ID先删除sys_role_user里面原来的数据
        this.roleMapper.deleteRoleUserByUserIds(Arrays.asList(userId));
        for (Long roleId : roleIds) {
            this.roleMapper.saveRoleUser(userId,roleId);
        }
    }
}




